﻿using UnityEngine;
using UnityEngine.EventSystems;

namespace JH
{
    /// <summary>
    ///  模拟点击
    /// </summary>
    public static class SimulateClick
    {
        /// <summary>
        ///  尽可能模拟用户的点击操作，对比调用OnSubmit方法或者onClick.Invoke会执行更多的点击逻辑
        /// </summary>
        public static void SimulateClickEm(this UnityEngine.UI.Button button)
        {
            var pointer = new PointerEventData(EventSystem.current)
            {
                // 设置指针位置（按钮中心）
                position = RectTransformUtility.WorldToScreenPoint(Camera.current, button.transform.position)
            };
            ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents.submitHandler);
        }
    }
}